package node.block.stmt;

import midcode.MidCode;
import midcode.MidcodeGenerator;
import midcode.OPType;
import node.Node;
import node.exp.Exp;
import node.exp.LVal;
import symbol.Symbol;

public class AssignStmt implements Node {
    private final LVal lVal;
    private final Exp exp;

    public AssignStmt(Exp exp, LVal lVal) {
        this.exp = exp;
        this.lVal = lVal;
    }

    @Override
    public Node getFather() {
        return father;
    }

    @Override
    public String genMidCode() {
        MidcodeGenerator mg = MidcodeGenerator.getInstance();
        Symbol symbol = lVal.symbol;
        if (symbol.dim == 0) {
            mg.addMidcode(new MidCode(OPType.ASSIGN, symbol.getName(), null, exp.genMidCode()));
        } else if (symbol.dim == 1) {
            mg.addMidcode(new MidCode(OPType.SW_ARRAY, symbol.getName(), lVal.index_1.genMidCode(), exp.genMidCode()));
        } else {
            assert symbol.dim == 2;
            mg.genArrayIndex(symbol, lVal.index_1.genMidCode(), lVal.index_2.genMidCode(), exp.genMidCode());
        }
        return "";
    }
}

